﻿using System;
using System.Windows;
using System.Windows.Media;

namespace VisualView.Series
{
    public class HistogramXYSeries<TX, TY> : BaseXYSeries<TX, TY>
    {
        const byte DefaultColumnWidth = 0x04;
        /// <summary>
        /// 柱子宽度,半径
        /// </summary>
        public byte ColumnWidth
        {
            get { return (byte)GetValue(ColumnWidthProperty); }
            set { SetValue(ColumnWidthProperty, value); }
        }
        public static readonly DependencyProperty ColumnWidthProperty = DependencyProperty.Register("ColumnWidth", typeof(byte), typeof(HistogramXYSeries<TX, TY>), new PropertyMetadata(DefaultColumnWidth, PropertyChangedNeedRedraw));

        protected override Geometry CreateGeometry(int count, Func<int, double> getXFunc, Func<int, double> getYFunc)
        {
            byte columnWidth = DefaultColumnWidth;
            double height = 0d;
            this.Dispatcher.Invoke(() =>
            {
                if (ColumnWidth > 0x00) columnWidth = ColumnWidth;
                height = base.ActualHeight;
            });
            var geometry = new StreamGeometry();
            using (var sc = geometry.Open())
            {
                for (int i = 0; i < count; i++)
                {
                    var x = getXFunc(i);
                    var y = getYFunc(i);
                    sc.BeginFigure(new Point(x - columnWidth, y), true, true);
                    sc.PolyLineTo(new Point[] { new Point(x + columnWidth, y), new Point(x + columnWidth, height), new Point(x - columnWidth, height) }, true, true);
                }
            }
            return geometry;
        }
    }

    public class HistogramDoubleXYSeries : HistogramXYSeries<double, double> { }
    public class HistogramDoubleXDateTimeYSeries : HistogramXYSeries<double, DateTime> { }
    public class HistogramDoubleXTimeSpanYSeries : HistogramXYSeries<double, TimeSpan> { }

    public class HistogramDateTimeXYSeries : HistogramXYSeries<DateTime, DateTime> { }
    public class HistogramDateTimeXDoubleYSeries : HistogramXYSeries<DateTime, double> { }
    public class HistogramDateTimeXTimeSpanYSeries : HistogramXYSeries<DateTime, TimeSpan> { }

    public class HistogramTimeSpanXYSeries : HistogramXYSeries<TimeSpan, TimeSpan> { }
    public class HistogramTimeSpanXDoubleY : HistogramXYSeries<TimeSpan, double> { }
    public class HistogramTimeSpanXDateTimeY : HistogramXYSeries<TimeSpan, DateTime> { }
}
